﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Region.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The region.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// The region.
    /// </summary>
    [DataContract]
    public class Region
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the additional information.
        /// </summary>
        [DataMember]
        public string AdditionalInformation { get; set; }

        /// <summary>
        /// Gets or sets the country.
        /// </summary>
        [DataMember]
        public Country Country { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="reg">
        /// The reg.
        /// </param>
        /// <returns>
        /// The <see cref="Region"/>.
        /// </returns>
        public static Region Convert(Models.Domain.Region reg)
        {
            if (reg != null)
            {
                var dto = new Region { ID = reg.ID, Name = reg.Name, Country = Country.Convert(reg.Country) };
                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Locations;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The regions query.
    /// </summary>
    [DataContract]
    public class RegionsQuery : IQuery
    {
        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new RegionActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The regions result.
    /// </summary>
    [DataContract]
    public class RegionsResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RegionsResult"/> class.
        /// </summary>
        public RegionsResult()
        {
            this.List = new List<Region>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Region> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Locations;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create region command.
    /// </summary>
    [DataContract]
    public class CreateRegionCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Region Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new RegionActioner(hsession, activeUser);
            actioner.CreateRegion(this);
        }

        #endregion
    }

    /// <summary>
    /// The update region command.
    /// </summary>
    [DataContract]
    public class UpdateRegionCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Region Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new RegionActioner(hsession, activeUser);
            actioner.UpdateRegion(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete region command.
    /// </summary>
    [DataContract]
    public class DeleteRegionCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new RegionActioner(hsession, activeUser);
            actioner.DeleteRegion(this);
        }

        #endregion
    }
}